# Look at specifics of Comparative Manifestos Dataset
# JAPAN
# 16 Oct 2008

rm(list=ls(all=TRUE))
library(foreign)

require(foreign)
require(Hmisc)


# Set your working directory here 
setwd("/Volumes/sproksch/UCLA Projects/Japanese Manifestos")

data<-read.dta("cmp_ajps2.dta") 
dataintpeace<-read.dta("cmp_ajps2withIntpeace.dta")
data<-data[data[,2]==71,]
dataintpeace<-dataintpeace[dataintpeace[,2]==71,]

#####################
# PLOT LEFT-RIGHT
#####################

 
attach(data)
quartz()

rileCIlo = rileMean - 2*rileSE
rileCIhi = rileMean + 2*rileSE
dfjap <- data.frame(cmp_party, rileMean, rileCIlo, rileCIhi, cmp_edate)

partylabels<-unique(cmp_party)

datestring<-unique(substring(as.Date(dfjap$cmp_edate,origin="1970-01-01"),1,4))
uniqueedate<-unique(dfjap$cmp_edate)


#### Plot only extreme parties
plot(datestring,seq(-100,100,length.out=length(datestring)),type="n",ylab="CMP Left-Right (with confidence intervals)",xlab="Election Year",main="Japan CMP Left-Right Data\nMost Extreme Parties in Each Election with CI")

for (i in 1:length(uniqueedate)){



df <- dfjap[cmp_edate==uniqueedate[i],]
dates<-substring(as.Date(df$cmp_edate,origin="1970-01-01"),1,4)
dates<-unique(as.numeric(dates))

minparty<-df[df$rileMean==min(df$rileMean),]
maxparty<-df[df$rileMean==max(df$rileMean),]


points(dates, minparty$rileMean,col="red",pch=19)
points(dates,maxparty$rileMean,col="blue",pch=19)
segments(dates,minparty$rileCIlo, dates ,minparty$rileCIhi,col="red")
segments(dates-0.4, minparty$rileCIlo,dates+0.4, minparty$rileCIlo,col="red")
segments(dates-0.4, minparty$rileCIhi,dates+0.4, minparty$rileCIhi,col="red")
segments(dates,maxparty$rileCIlo, dates ,maxparty$rileCIhi,col="blue")
segments(dates-0.4, maxparty$rileCIlo,dates+0.4, maxparty$rileCIlo,col="blue")
segments(dates-0.4, maxparty$rileCIhi,dates+0.4, maxparty$rileCIhi,col="blue")
}


### Plot all parties

#JCP Nihon Kyosan-to (Japan Communist Party) 1960-2000 14 71220 
#JSP Nihon Shakai-to (Japan Socialist Party) 1960-2000 14 71320 
#DSP Minshu-Shakai-to (Democratic Socialist Party) 1960-1993 12 71321 
#SDF Shaminren (Social Democratic Federation) 1979-1990 4 71322 
#CGP Komei-to (Clean Government Party) 1967-2000 11 71530 
#LDP Jiyu-Minshu-to (Liberal Democratic Party) 1960-2000 14 71620 
#NLC Shin Jiyu Club (New Liberal Club) 1976-1986 5 71621 
#JRP Shinsei-to (Japan Renewal Party) 1993 1 71622 
#NFP Sinshin (New Frontier Party) 1996 1 71623 
#DPJ Minshu (Democratic Party of Japan) 1996+2000 2 71624 
#LP Jiyo-to (Liberal Party) 2000 1 71625 
#NCP Hoshu-to (New Conservative Party) 2000 1 71626 
#NP Sakigake (New Party) 1993+1996 2 71951 
#JNP Nihon Shin-to (Japan New Party) 1993 1 71952 


plot(datestring,seq(-100,100,length.out=length(datestring)),type="n",ylab="CMP Left-Right (with confidence intervals)",xlab="Election Year",main="Japan CMP Left-Right Data\nMajor Parties",xaxt="n",yaxt="n")

axis(1,at=datestring,cex.axis=0.9,cex=0.8)
axis(2,at=c(-100,-75,-50,-25,0,25,50,75,100),las=1)


majorpartylabels<-c(71220,71320,71321,71530,71620,71621)
linetype<-c(1,2,3,4,1,2)
colvec<-rainbow(length(majorpartylabels),start=0.7,end=0.15)
#colvec<-gray(0:length(majorpartylabels) / length(majorpartylabels))

for (i in 1:length(majorpartylabels)){



df <- dfjap[cmp_party== majorpartylabels[i],]
dates<-substring(as.Date(df$cmp_edate,origin="1970-01-01"),1,4)
dates<-unique(as.numeric(dates))


lines(dates, df$rileMean,col=colvec[i],lwd=3,lty=linetype[i])
#lines(dates, df$rileCIlo,col=colvec[i],lty=linetype[i],lwd=1.5)
#lines(dates, df$rileCIhi,col=colvec[i],lty=linetype[i],lwd=1.5)

#segments(dates,df$rileCIlo, dates , df$rileCIhi,col=colvec[i],lwd=3)
#segments(dates-0.4, df$rileCIlo,dates+0.4, df$rileCIlo,col=colvec[i],lwd=3)
#segments(dates-0.4, df$rileCIhi,dates+0.4, df$rileCIhi,col=colvec[i],lwd=3)


}


legend(1994,95,legend=c("JCP","JSP","DSP","CGP","LDP","NLC"),lty=linetype,lwd=2.5,col=colvec)

##########################################
### Plot left-right LDP and JSP with CIs
##########################################

colvectwo<-c(colvec[2],colvec[5])
plot(datestring,seq(-100,100,length.out=length(datestring)),type="n",ylab="CMP Left-Right (with confidence intervals)",xlab="Election Year",main="Japan CMP Left-Right Data\nLDP and JSP in Comparison",xaxt="n",yaxt="n")

axis(1,at=datestring,cex.axis=0.9,cex=0.8)
axis(2,at=c(-100,-75,-50,-25,0,25,50,75,100),las=1)


majorpartylabels<-c(71320,71620)
linetype<-c(2,1)
colvec<-colvectwo
#colvec<-gray(0:length(majorpartylabels) / length(majorpartylabels))

for (i in 1:length(majorpartylabels)){



df <- dfjap[cmp_party== majorpartylabels[i],]
dates<-substring(as.Date(df$cmp_edate,origin="1970-01-01"),1,4)
dates<-unique(as.numeric(dates))


lines(dates, df$rileMean,col=colvec[i],lwd=3,lty=linetype[i])
#lines(dates, df$rileCIlo,col=colvec[i],lty=linetype[i],lwd=1.5)
#lines(dates, df$rileCIhi,col=colvec[i],lty=linetype[i],lwd=1.5)

segments(dates,df$rileCIlo, dates , df$rileCIhi,col=colvec[i],lwd=3)
segments(dates-0.4, df$rileCIlo,dates+0.4, df$rileCIlo,col=colvec[i],lwd=3)
segments(dates-0.4, df$rileCIhi,dates+0.4, df$rileCIhi,col=colvec[i],lwd=3)


}


legend(1994,95,legend=c("JSP","LDP"),lty=linetype,lwd=2.5,col=colvec)


detach(data)

##########################################
### Plot international peace dimension
##########################################

attach(dataintpeace)



intpeaceCIlo = intpeaceMean - 2*intpeaceSE
intpeaceCIhi = intpeaceMean + 2*intpeaceSE
dfjap <- data.frame(cmp_party, intpeaceMean, intpeaceCIlo, intpeaceCIhi, cmp_edate)

partylabels<-unique(cmp_party)

datestring<-unique(substring(as.Date(dfjap$cmp_edate,origin="1960-01-01"),1,4))
uniqueedate<-unique(dfjap$cmp_edate)



### Plot all parties

#JCP Nihon Kyosan-to (Japan Communist Party) 1960-2000 14 71220 
#JSP Nihon Shakai-to (Japan Socialist Party) 1960-2000 14 71320 
#DSP Minshu-Shakai-to (Democratic Socialist Party) 1960-1993 12 71321 
#SDF Shaminren (Social Democratic Federation) 1979-1990 4 71322 
#CGP Komei-to (Clean Government Party) 1967-2000 11 71530 
#LDP Jiyu-Minshu-to (Liberal Democratic Party) 1960-2000 14 71620 
#NLC Shin Jiyu Club (New Liberal Club) 1976-1986 5 71621 
#JRP Shinsei-to (Japan Renewal Party) 1993 1 71622 
#NFP Sinshin (New Frontier Party) 1996 1 71623 
#DPJ Minshu (Democratic Party of Japan) 1996+2000 2 71624 
#LP Jiyo-to (Liberal Party) 2000 1 71625 
#NCP Hoshu-to (New Conservative Party) 2000 1 71626 
#NP Sakigake (New Party) 1993+1996 2 71951 
#JNP Nihon Shin-to (Japan New Party) 1993 1 71952 


plot(datestring,seq(0,100,length.out=length(datestring)),type="n",ylab="CMP International Peace (with confidence intervals)",xlab="Election Year",main="Japan CMP International Peace Data\nMajor Parties",xaxt="n",yaxt="n")

axis(1,at=datestring,cex.axis=0.9,cex=0.8)
axis(2,at=c(-100,-75,-50,-25,0,25,50,75,100),las=1)


majorpartylabels<-c(71220,71320,71321,71530,71620,71621)
linetype<-c(1,2,3,4,1,2)
colvec<-rainbow(length(majorpartylabels),start=0.7,end=0.15)
#colvec<-gray(0:length(majorpartylabels) / length(majorpartylabels))

for (i in 1:length(majorpartylabels)){



df <- dfjap[cmp_party== majorpartylabels[i],]
dates<-substring(as.Date(df$cmp_edate,origin="1960-01-01"),1,4)
dates<-unique(as.numeric(dates))


lines(dates, df$intpeaceMean,col=colvec[i],lwd=3,lty=linetype[i])
#lines(dates, df$intpeaceCIlo,col=colvec[i],lty=linetype[i],lwd=1.5)
#lines(dates, df$intpeaceCIhi,col=colvec[i],lty=linetype[i],lwd=1.5)

#segments(dates,df$intpeaceCIlo, dates , df$intpeaceCIhi,col=colvec[i],lwd=3)
#segments(dates-0.4, df$intpeaceCIlo,dates+0.4, df$intpeaceCIlo,col=colvec[i],lwd=3)
#segments(dates-0.4, df$intpeaceCIhi,dates+0.4, df$intpeaceCIhi,col=colvec[i],lwd=3)


}
legend(1994,95,legend=c("JCP","JSP","DSP","CGP","LDP","NLC"),lty=linetype,lwd=2.5,col=colvec)


#####################################
# Plot JSP and LDP IntPeace with CIs
#####################################


plot(datestring,seq(0,100,length.out=length(datestring)),type="n",ylab="CMP International Peace (with confidence intervals)",xlab="Election Year",main="Japan CMP International Peace Data\nLDP and JSP in Comparison",xaxt="n",yaxt="n")

axis(1,at=datestring,cex.axis=0.9,cex=0.8)
axis(2,at=c(-100,-75,-50,-25,0,25,50,75,100),las=1)

majorpartylabels<-c(71320,71620)
linetype<-c(2,1)
colvec<-colvectwo
#colvec<-gray(0:length(majorpartylabels) / length(majorpartylabels))

for (i in 1:length(majorpartylabels)){



df <- dfjap[cmp_party== majorpartylabels[i],]
dates<-substring(as.Date(df$cmp_edate,origin="1960-01-01"),1,4)
dates<-unique(as.numeric(dates))


lines(dates, df$intpeaceMean,col=colvec[i],lwd=3,lty=linetype[i])
#lines(dates, df$intpeaceCIlo,col=colvec[i],lty=linetype[i],lwd=1.5)
#lines(dates, df$intpeaceCIhi,col=colvec[i],lty=linetype[i],lwd=1.5)

segments(dates,df$intpeaceCIlo, dates , df$intpeaceCIhi,col=colvec[i],lwd=3)
segments(dates-0.4, df$intpeaceCIlo,dates+0.4, df$intpeaceCIlo,col=colvec[i],lwd=3)
segments(dates-0.4, df$intpeaceCIhi,dates+0.4, df$intpeaceCIhi,col=colvec[i],lwd=3)


}

legend(1994,95,legend=c("JSP","LDP"),lty=linetype,lwd=2.5,col=colvec)

